package demo;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

public class Main {

    public static void main(String[] args) throws IOException {
//        String pwd = System.getProperty("user.dir");
//        System.out.println(pwd);
//        for (Map.Entry<String, String> entry : System.getenv().entrySet()) {
//            System.out.printf("Key: %s, Value: %s\n", entry.getKey(), entry.getValue());
//        }
//
//        System.out.println();
//        Enumeration enumeration = System.getProperties().propertyNames();
//        while (enumeration.hasMoreElements()) {
//            String key = (String) enumeration.nextElement();
//            String value = System.getProperty(key);
//            System.out.printf("Key: %s, Value: %s\n", key, value);
//        }

//        Path workPath = Paths.get(pwd).resolve("work");
//        System.out.println(workPath); // pwd与"work"构成相对路径
//        workPath = Paths.get(pwd).resolve("/home/tengyft");
//        System.out.println(workPath); // 使用绝对路径
//
//        Path tmpPath = Paths.get(pwd).resolveSibling("tmp");
//        System.out.println(tmpPath);

        Path path = Paths.get("data.txt");
        byte[] rawBytes = Files.readAllBytes(path);
        String contents = new String(rawBytes, StandardCharsets.UTF_8);
        System.out.println(contents);

        List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
        for (String line : lines) {
            System.out.println(line);
        }

        BasicFileAttributes attributes = Files.readAttributes(path, BasicFileAttributes.class);
        System.out.println(path.toAbsolutePath() + ":");
        System.out.println("Created Time:       " + attributes.creationTime());
        System.out.println("Last Access Time:   " + attributes.lastAccessTime());
        System.out.println("Last Modified Time: " + attributes.lastModifiedTime());
        System.out.println("File Key:  " + attributes.fileKey());
        System.out.println("File Size: " + attributes.size() + " bytes");

//        String curWorkDir = System.getProperty("user.dir");
//        Files.walk(Paths.get(curWorkDir))
//                .forEach(System.out::println);

        Files.walkFileTree(Paths.get("/"), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                System.out.println(dir);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                return FileVisitResult.SKIP_SUBTREE;
            }
        });
    }
}
